﻿using BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

//安装包
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;

namespace Hospital.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MeditionController : ControllerBase
    {
        /// <summary>
        /// 依赖注入MeditionBLL
        /// </summary>
        MeditionBLL bll;
        ILogger<MeditionController> logger;

        public MeditionController(MeditionBLL bll, ILogger<MeditionController> logger)
        {
            this.bll = bll;
            this.logger = logger;
        }

        /// <summary>
        /// 显示用药列表
        /// </summary>
        /// <param name="totalCount">总条数</param>
        /// <param name="totalPage">总页数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="doctorName">医生姓名</param>
        /// <param name="wardId">科室</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public IActionResult ShowMedition(int pageIndex = 1, int pageSize = 2, string? doctorName = "", string? wardName = "")
        {
            try
            {
                int totalCount = 0;
                int totalPage = 0;
                IQueryable<MeditionModel> list=bll.ShowMedition(out totalCount, out totalPage,pageIndex,pageSize,doctorName, wardName);
                logger.LogInformation($"{DateTime.Now}显示用药列表成功");
                return Ok(new
                {
                    totalCount=totalCount,
                    totalPage=totalPage,
                    list=list
                });
            }
            catch (Exception ex)
            {
                logger.LogError("获取用药列表失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 绑定下拉科室
        /// </summary>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public IQueryable<WardModel> GetWard()
        {
            try
            {
                logger.LogInformation($"{DateTime.Now}获取科室列表成功");
                return bll.GetWard();
            }
            catch (Exception ex)
            {
                logger.LogError("获取科室列表失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 删除某条用药数据
        /// </summary>
        /// <param name="id">查询ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpDelete]
        public async Task<int> DelMedition(int id)
        {
            try
            {
                logger.LogInformation($"{DateTime.Now}删除成功");
                return await bll.DelMedition(id);    
            }
            catch (Exception ex)
            {
                logger.LogError("获取科室列表失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改状态为已完成
        /// </summary>
        /// <param name="ids">ID数组</param>
        /// <returns>受影响返回的行数</returns>
        [HttpPut]
        public async Task<int> UpdState(string ids)
        {
            try
            {
                logger.LogInformation($"{DateTime.Now}修改状态成功");
                return await bll.UpdState(ids);
            }
            catch (Exception ex)
            {
                logger.LogError("修改状态失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 反填用药
        /// </summary>
        /// <param name="id">查询ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public IQueryable<MeditionModel> GetMedition(int id)
        {
            try
            {
                logger.LogInformation($"{DateTime.Now}反填成功");
                return bll.GetMedition(id);
            }
            catch (Exception ex)
            {
                logger.LogError("反填失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改用药
        /// </summary>
        /// <param name="m">用药表实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<int> Updasync(MeditionModel m)
        {
            try
            {
                logger.LogInformation($"{DateTime.Now}修改成功");
                return await bll.Updasync(m);
            }
            catch (Exception ex)
            {
                logger.LogError("修改失败", ex.Message);
                throw;
            }
        }
    }
}
